import {observable , action, runInAction} from 'mobx'
import axios from 'axios'
import { message } from 'antd'
import {apiUrl} from '../api/constant'

class logStore {
    @observable shopLogList
    @observable loading
    @observable shopItemLogList
    @observable categoryLogList
    @observable inventoryLogList
    

    constructor() {
        this.shopLogList = []
        this.loading = false
        this.shopItemLogList = []
        this.categoryLogList = []
        this.inventoryLogList = []
    }

    //门店日志
    @action.bound getShopLogList = async (page, pageSize, taskType, operator, shopId, channelId, channelShopId, startDate, endDate, processStatus) =>{
        this.loading = true 
        const data = encodeURI(JSON.stringify({
            page : page || 1,
            pageSize: pageSize || 10,
            taskType : taskType || '',
            operator: operator || '',
            shopId: shopId || '',
            channelId: channelId || '',
            startDate: startDate || '',
            endDate: endDate || '',
            channelShopId: channelShopId || '',
            processStatus: processStatus
        }))
        const result = await axios({
            method: 'get',
            url: `${apiUrl}/log/getChannelShopLog?data=${data}`,
        })
        if(result.status !==200){
            message.error('数据加载失败')
            return []
        }
        runInAction (() =>{
            if(result.data.data){
                result.data.data.forEach((item,index) => {
                    item.key = index
                })
                this.shopLogList = result.data
                this.loading = false
            }else{
                message.error(result.data.err)
            }
        })
    }

    //商品日志
    @action.bound getGoodsLogList = async (page, pageSize, taskType, operator, shopId, channelId, itemCode, startDate, endDate, processStatus, batchId ) =>{
        this.loading = true 
        const data = encodeURI(JSON.stringify({
            page : page || 1,
            pageSize: pageSize || 10,
            taskType : taskType || '',
            operator: operator || '',
            shopId: shopId || '',
            channelId: channelId || '',
            startDate: startDate || '',
            endDate: endDate || '',
            itemCode: itemCode || '',
            processStatus: processStatus,
            batchId: batchId
        }))
        const result = await axios({
            method: 'get',
            url: `${apiUrl}/log/getO2oGoodsLog?data=${data}`,
        })
        if(result.status !==200){
            message.error('数据加载失败')
            return []
        }
        runInAction (() =>{
            if(result.data.data){
                result.data.data.forEach((item,index) => {
                    item.key = index
                })
                this.shopItemLogList = result.data
                this.loading = false
            }else{
                message.error(result.data.err)
            }
        })
    }

    //类别日志
    @action.bound getCategoryLogList = async (page, pageSize, taskType, operator, shopId, channelId, startDate, endDate, processStatus) =>{
        this.loading = true 
        const data = encodeURI(JSON.stringify({
            page : page || 1,
            pageSize: pageSize || 10,
            taskType : taskType || '',
            operator: operator || '',
            shopId: shopId || '',
            channelId: channelId || '',
            startDate: startDate || '',
            endDate: endDate || '',
            processStatus: processStatus
        }))
        const result = await axios({
            method: 'get',
            url: `${apiUrl}/log/getChannelCategoryLog?data=${data}`,
        })
        if(result.status !==200){
            message.error('数据加载失败')
            return []
        }
        runInAction (() =>{
            if(result.data.data){
                result.data.data.forEach((item,index) => {
                    item.key = index
                })
                this.categoryLogList = result.data
                this.loading = false
            }else{
                message.error(result.data.err)
            }
        })
    }

    //库存日志
    @action.bound getInventoryLogList = async (page, pageSize,channelShopId, itemCode, shopId, channelId, startDate, endDate, processStatus, batchId) =>{
        this.loading = true 
        const data = encodeURI(JSON.stringify({
            page : page || 1,
            pageSize: pageSize || 10,
            channelShopId : channelShopId || '',
            itemCode: itemCode || '',
            shopId: shopId || '',
            channelId: channelId || '',
            startDate: startDate || '',
            endDate: endDate || '',
            processStatus: processStatus || '',
            batchId: batchId
        }))
        const result = await axios({
            method: 'get',
            url: `${apiUrl}/log/getO2oInventoryLog?data=${data}`,
        })
        if(result.status !==200){
            message.error('数据加载失败')
            return []
        }
        runInAction (() =>{
            if(result.data.data){
                result.data.data.forEach((item,index) => {
                    item.key = index
                })
                this.inventoryLogList = result.data
                this.loading = false
            }else{
                message.error(result.data.err)
            }
        })
    }
}

export default new logStore()